#ifndef REWARDSMODEL_H
#define REWARDSMODEL_H

#include <QObject>
#include <QJsonObject>
#include <qabstractitemmodel.h>

struct Reward
{
    QString name;
    int points;

    Reward(const QString rewardsName, int needPoints)
        : name{rewardsName}
        , points{needPoints}
    {}


    Reward(const QJsonObject &jsonObj)
    {
        name = jsonObj["name"].toString();
        points = jsonObj["points"].toInt();
    }

    QJsonObject toJson() const {
        QJsonObject jsonObj;
        jsonObj["name"] = name;
        jsonObj["points"] = points;
        return jsonObj;
    }
};

class RewardsModel : public QAbstractListModel
{
    Q_OBJECT
public:
    // 定义要使用的角色名称

    enum RoleName {
        NameRole = Qt::UserRole + 1,
        Points = Qt::UserRole + 2
    };

    explicit RewardsModel(QObject *parent = nullptr);

    Q_INVOKABLE void addReward(QString text, int points);
    Q_INVOKABLE void removeReward(int index);

    Q_INVOKABLE void exchangeReward(int index);

    Q_INVOKABLE void saveRewardData();
    Q_INVOKABLE void loadRewardData();

    virtual int rowCount(const QModelIndex &parent) const override;
    virtual QVariant data(const QModelIndex &index, int role) const override;

signals:

protected:
    virtual QHash<int, QByteArray> roleNames() const override;

private:
    QList<Reward> m_rewards;
    QHash<int, QByteArray> m_roleNames;

    // JSON 存储
    QString getDataFilePath();
    void createInitialJsonFile();
    bool loadFromJsonFile();
};

#endif // REWARDSMODEL_H
